<html>
<script src="../../../resources/js-test.js"></script>
<style>
#iframe1 {
  width: 200px;
  height: 200px;
  margin: 0;
  padding: 0;
  border: 1px solid black;
  position:absolute;
  left:50px;
  top:50px;
  background: red;
}

#output
{
position:absolute;
top:250px;
}

</style>
<body onload="runTest()">
<iframe id="iframe1" src="./resources/multi-touch-inside-nested-iframes2.html"></iframe>
<div id="output">
<p id="description"></p>
<div id="console"></div>
</div>
<script type="text/javascript">
description('Test multi-touch is sent to the same iframe.');

var touchEventCount = 0;
var title = 0;

function onTouch(event, receiver)
{
    ev = event;
    touchreceiver = receiver;
    switch (touchEventCount) {
        case 0:
            shouldBeEqualToString('ev.type', 'touchstart');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "50");
            shouldBe("ev.touches[0].pageY", "50");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe1');
            break;
        case 1:
            shouldBeEqualToString('ev.type', 'touchmove');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "2");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "200");
            shouldBe("ev.touches[0].pageY", "200");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBeEqualToString('ev.touches[1].target.nodeName', '#document');
            shouldBeEqualToString('ev.touches[1].target.title', 'iframe1');
            break;
        case 2:
            shouldBeEqualToString('ev.type', 'touchend');
            shouldBeEqualToString('touchreceiver', 'iframe1');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "0");
            shouldBe('ev.changedTouches[0].identifier', '0');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe1');
            shouldBe('ev.touches[0].identifier', '1');
            shouldBeEqualToString('ev.touches[0].target.nodeName', '#document');
            shouldBeEqualToString('ev.touches[0].target.title', 'iframe1');
            break;
        case 3:
            shouldBeEqualToString('ev.type', 'touchstart');
            shouldBeEqualToString('touchreceiver', 'iframe2');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "1");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "30");
            shouldBe("ev.touches[0].pageY", "30");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe2');
            break;
        case 4:
            shouldBeEqualToString('ev.type', 'touchstart');
            shouldBeEqualToString('touchreceiver', 'iframe2');
            shouldBe("ev.changedTouches.length", "1");
            shouldBe("ev.touches.length", "2");
            shouldBe("ev.targetTouches.length", "1");
            shouldBe("ev.touches[0].pageX", "30");
            shouldBe("ev.touches[0].pageY", "30");
            shouldBeEqualToString('ev.touches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.targetTouches[0].target.ownerDocument.title', 'iframe2');
            break;
        case 5:
            shouldBeEqualToString('ev.type', 'touchend');
            shouldBeEqualToString('touchreceiver', 'iframe2');
            shouldBe("ev.changedTouches.length", "2");
            shouldBe("ev.touches.length", "0");
            shouldBe("ev.targetTouches.length", "0");
            shouldBeEqualToString('ev.changedTouches[0].target.ownerDocument.title', 'iframe2');
            shouldBeEqualToString('ev.changedTouches[1].target.ownerDocument.title', 'iframe2');
            isSuccessfullyParsed();
            testRunner.notifyDone();
    }
    touchEventCount++;
}

function runTest() {
    if (window.eventSender) {
        eventSender.clearTouchPoints();
        // Touch the center of innter iframe. 50px is offset to outer iframe in main frame,
        // 2px for the iframe borders, 100px to get to centre.
        debug('First touch is on inner iframe.');
        eventSender.addTouchPoint(152, 152);
        eventSender.touchStart();
        debug('');

        debug('Second touch is on outer iframe, nothing should happen.');
        eventSender.addTouchPoint(71, 71);
        eventSender.touchStart();
        debug('');

        debug('Moves the first touch outside inner iframe.');
        eventSender.updateTouchPoint(0, 302, 302);
        eventSender.touchMove();
        debug('');

        debug('Release the first touch.');
        eventSender.releaseTouchPoint(0);
        eventSender.touchEnd();
        debug('');

        debug('Third touch is on outer iframe, nothing should happen.');
        eventSender.addTouchPoint(81, 81);
        eventSender.touchStart();
        debug('');

        debug('Release all touches on outer iframe, and touch outer iframe again.');
        eventSender.releaseTouchPoint(0);
        eventSender.releaseTouchPoint(1);
        eventSender.touchEnd();
        eventSender.addTouchPoint(81, 81);
        eventSender.touchStart();
        debug('');

        debug('Touch inner iframe, this will trigger onTouch as it is inside outer iframe.');
        eventSender.addTouchPoint(152, 152);
        eventSender.touchStart();
        debug('');

        debug('Release all touches.');
        eventSender.releaseTouchPoint(0);
        eventSender.releaseTouchPoint(1);
        eventSender.touchEnd();
    } else {
       debug('This test requires DRT.');
    }
}

if (window.testRunner) {
    testRunner.waitUntilDone();
}

function getTitle() {
    title++;
    return  'iframe' + title;
}
</script>
</body>
</html>
